package coder.taiji.test.springnetty.tcp.outbound;

import coder.taiji.test.springnetty.tcp.dto.TestProtocolDTO;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;

/**
 * describe:
 *
 * @author helloworldyu
 * @date 2018/3/14
 */
public class SecondOutboundChannel extends ChannelOutboundHandlerAdapter {
	@Override
	public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
		System.out.println("=================secondoutbound=================");
		//传递到下一个 outboundHandlerAdapter
		TestProtocolDTO testProtocolDTO = (TestProtocolDTO) msg;
		Integer type = testProtocolDTO.getType();
		String info = testProtocolDTO.getInfo();

		ByteBuf buffer = ctx.alloc().buffer();
		buffer.writeByte(type).writeBytes(info.getBytes());
		super.write(ctx, buffer, promise);
		promise.addListener(
				(ChannelFutureListener) future -> System.out.println("future: FirstOutboundHandler1 收到 write() 成功的消息"));
	}
}